# -*- coding: utf-8 -*-

#    Copyright (C) 2014 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdCollect.

#    OdCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdCollect.  If not, see <http://www.gnu.org/licenses/>.

import datetime
from web_lib.page import Page
from web_lib.webutils import Response, expose , json , salvaInSessione, leggiInSessione , permalinkaTitle
from dao.User import User as Utente
from dao.Ente import Ente
from dao.UtenteEnte import UtenteEnte
import Environment
from lib.utils import prep2json
from webEnv import *


@expose('/pg/cu_UtenteEnte/<action>')
@expose('/pg/cu_UtenteEnte/')
def customUtenteEnte(req, action=None):
    """

    """
    def __show__(req, action=None):
        utenti = Utente().select(batchSize=None)
        enti = Ente().select(batchSize=None)
        uteent = UtenteEnte().select(batchSize=None)
        pageData = {'file' : "/pg/cu_UtenteEnte",
                    "utenti":utenti,
                    "enti":enti,
                    "uteent":uteent,
            }
        return Page(req).render(pageData)

    def __create__update__(req, action=None):
        """Aggiunge una riga alle pesate"""
        print "VEDIAMO", req.form.to_dict()
        idUtente = req.form.get("id_utente")
        idUtenteEnte = req.form.getlist("id_utenteente")
        if idUtente != "None":
            tai = UtenteEnte().select(id_utente=int(idUtente), batchSize = None)
        if tai:
            for t in tai:
                t.delete()
        for a in idUtenteEnte:
            d = UtenteEnte()
            d.id_utente = int(idUtente)
            d.id_ente = int(a)
            d.persist()

        url_red = "/pg/cu_UtenteEnte/"
        return Page(req).redirect(url_red)

    def __get_utenti_enti__(req, action=None):
        idUtente= req.args.get("id_utente")
        try:
            tai = UtenteEnte().select(id_utente=int(idUtente), batchSize = None)
        except:
            tai = []
        item = [str(d.id_ente) for d in tai]
        jasoned = json.dumps(item, ensure_ascii=False, encoding='utf8')
        return Response(jasoned)


    if action=="List":
        return __list__ (req, action=action)
    elif action=="show":
        return __show__(req, action=action)
    elif action=="Create" or action == "Update":
        return __create__update__(req, action=action)
    elif action=="Delete":
        return __delete__(req, action=action)
    elif action == "Edit":
        return __edit__(req, action=action)
    elif action == "GetUtenteEnte":
        return __get_utenti_enti__(req, action=action)
    else:
        return __show__(req, action=action)
